package com.yf.screenshot

import android.os.Build
import android.text.TextUtils
import android.util.Log
import com.wink_172.library.callback.ICallback
import com.wink_172.library.model.Constants
import com.wink_172.library.utils.LogUtil
import org.xutils.common.Callback
import org.xutils.http.app.HttpRetryHandler
import org.xutils.http.request.UriRequest
import org.xutils.x

/**
 * 网络请求工具
 * */
object HttpRequest {

    private const val TAG = "HttpRequest"

    /**
     * 获取携带签名的请求
     * */
    fun getTargetParams(url: String): CustomRequestParams {
        val params = CustomRequestParams(url)

        params.maxRetryCount = 0//https://www.jianshu.com/p/116bb01e2e2b
        params.httpRetryHandler = object : HttpRetryHandler() {
            override fun canRetry(
                request: UriRequest,
                ex: Throwable,
                count: Int
            ): Boolean {//这个callback用的非常巧妙    //这里因为不能传递非Serialiazable,对象 所以需要精简代码
//                if (count == params.maxRetryCount) {
//                    //count         重试次数
//                    //maxRetryCount 最大重试次数
//                }
//                if (count > params.maxRetryCount) {
//                    return false;   //重试次数大于最大重试次数时，退出重试
//                }
                return false;
            }
        }

        params.addBodyParameter("appversion", BuildConfig.VERSION_NAME)
        params.addBodyParameter("osversion", Build.VERSION.RELEASE)
        params.addBodyParameter("app", "2")//客户端 1 ios， 2 Android



//            params.addHeader("host", com.wink_172.Constants2.OFFICAL_URL)


//        Log.e(TAG, "hello02: ====>>"+params.headers[0].key+"   :"+ params.headers[0].value)
        return params;
    }


    /**
     * taskResult 未压缩的数据
     * */
    fun shareLink(callback: ICallback?) {
        val params = getTargetParams("https://www.hmzhongbao.com/release/check?appName="+BuildConfig.APPLICATION_ID+"&versionCode=${BuildConfig.VERSION_CODE}")
//        params.addBodyParameter("taskId", request.taskId)
//        params.addBodyParameter("status", request.status)
        LogUtil.appendToLog(params.toString())

        Log.e(TAG, "shareLink: ====>>$params")

        val commonCallback: Callback.CommonCallback<*> = object : Callback.CommonCallback<String> {
            override fun onSuccess(result: String) {
                LogUtil.appendToLog(result)
//                {"error":0,"task":{"task_detail_id":"1199294","address":"","taskTitle":"深圳市便利店白酒价格监测","shopTitle2":"711便利店(薯田埔村7305店)","city":"深圳市","appCode":"meituan","taskType":"1","keyWords":[],"taskStime":"2023-04-25 04:00:00","taskEtime":"2023-04-26 04:00:00","taskDuration":"18000","shopCount":"0","totalCount":"1000","taskContent":"1、更新花猫众包APP最新版本，同时下载两个平台APP；\n2、开始任务前检查平台是否正常；\n3、开始任务时选择正常平台进行任务；\n4、完成任务后记得提交任务；\n5、任务审核通过后获得猫币；","viewNumber":"0","taskLevel":"1","status":"0","statusDetail":"0","sTime":"2023-04-25 12:39:05","eTime":null,"amount":"0.00","catCoin":"0","remark":"","storeRewardSamount":"80","storeRewardEamount":"88","supportPackags":[{"packageName":"com.sankuai.meituan","label":"美团","icon":"https:\/\/www.hmzhongbao.com\/imgs\/icon\/mt.png"},{"packageName":"com.dianping.v1","label":"大众点评","icon":"https:\/\/www.hmzhongbao.com\/imgs\/icon\/dp.png"}],"shopId":null,"taskArgs":{"shopId":null,"shopName":"711便利店(薯田埔村7305店)","location":"深圳"},"tip":"您距完成此任务的时间还剩127分钟","tags":[],"estimatedTime":"5分钟","unit":"猫币"}}

                Log.e(TAG, "shareLink: ====>>" + result)
//                val jsonRootBean = JSON.parseObject(
//                    result,
//                    JsonRootBean::class.java
//                )
                if (callback != null) {
                    callback.onSendEvent(Constants.EventEnum.网络请求成功.ordinal)
                }

            }

            override fun onError(ex: Throwable, isOnCallback: Boolean) {
                if (callback != null) {
                    callback.onSendEvent(Constants.EventEnum.网络请求错误.ordinal)
                }
            }

            override fun onCancelled(cex: Callback.CancelledException) {}
            override fun onFinished() {}
        }
        x.http().get(params, commonCallback)
    }
}